package com.dzf.service.impl;

import com.dzf.Util.SessionUtil;
import com.dzf.dao.BlogDao;
import com.dzf.entity.Blog;
import com.dzf.entity.BlogCont;
import com.dzf.entity.User;
import com.dzf.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class BlogServiceImpl implements BlogService {
    @Autowired
    BlogDao blogDao;
    @Autowired
    SessionUtil sessionUtil;

    /**
     * 分页
     * @param pageNum 数据库语句中的skip，从第几行开始查
     * @param num 查多少行
     * @return
     */
    @Override
    public List<Blog> findByPage(int pageNum, int num) {
        pageNum = (pageNum*num)-num;

        return null;
    }

    @Override
    public List<Blog> findByTops(String msg) {
        return blogDao.findByTops(msg);
    }


    @Override
    public Map<String, Object> findFuzzy(int pageNum, int num, Map<String,Object> likes) {
        pageNum = (pageNum*num)-num;
        String msg = (String) likes.get("msg");
        Map<String, Object> map = blogDao.findFuzzy(pageNum, num, msg);

        List<Blog> list = (List<Blog>) map.get("list");
        /**
         * 关键字高亮
         */
        if (likes!=null&& (int)likes.get("search")!=0)
        {

            for (Blog blog : list) {
                blog.setAuthor(blog.getAuthor().replace(msg,"<span style='color: red;font-weight: bold;'>"+msg+"</span>"));
                blog.setSummary(blog.getSummary().replace(msg,"<span style='color: red;font-weight: bold;'>"+msg+"</span>"));
                blog.setTitle(blog.getTitle().replace(msg,"<span style='color: red;font-weight: bold;'>"+msg+"</span>"));
            }
        }
        return map;
    }

    @Override
    public BlogCont findById(String blogid) {
        BlogCont blogCont = blogDao.findById(blogid);
        return blogCont;
    }

    @Override
    public boolean amend(BlogCont blogCont) {

        User user = sessionUtil.getUser();
        Blog blog = blogCont.getBlog();

        blog.setAuthor(user.getName());
        blog.setUid(user.getId());
        blog.setCreateTime(new Date());
        blog.setModifyTime(new Date());

        blog.setSummary(blog.getTitle()+"是由"+blog.getAuthor()+"进行编写");
        return blogDao.amend(blogCont);
    }

    @Override
    public Map<Object, Object> findByBlogType(String typeid, int pageNum){
        pageNum = (pageNum*10)-10;
        return blogDao.findByBlogType(typeid,pageNum);
    }

    @Override
    public List<Blog> findAll(String uid) {
        return blogDao.findAll(uid);
    }

    @Override
    public Blog findBlogById(String blogid) {
        return blogDao.findBlogById(blogid);
    }

    @Override
    public Boolean delBlogCont(Blog blog) {
        return blogDao.delBlogCont(blog);
    }
}
